package com.qky.controller.postController;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.qky.model.post.Post;
import com.qky.model.region.Province;
import com.qky.model.user.User;
import com.qky.service.baseService.BaseService;
import com.qky.service.postService.PostService;
import com.qky.service.regionService.RegionService;

@Controller
@RequestMapping("/post")
@SessionAttributes("loginId")  
public class PostController<E> {
	
	@Resource
	BaseService<E> baseService;
	@Resource
	RegionService regionService;
	
	
	@RequestMapping(value = "/upload",method=RequestMethod.POST)
	public String upload(@RequestParam(value="file", required = false) MultipartFile file,
			HttpServletRequest request, ModelMap model) {  
	    System.out.println("开始");  
	    String path = request.getSession().getServletContext().getRealPath("resources/upload");  
	    String fileName = file.getOriginalFilename();  
	    //String fileName = new Date().getTime()+".jpg";  
	    System.out.println(path);  
	    File targetFile = new File(path, fileName);  
	    if(!targetFile.exists()){  
	        targetFile.mkdirs();  
	    }  
	    //保存  
	    try {  
	        file.transferTo(targetFile);  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    }  
	    model.addAttribute("fileUrl", request.getContextPath()+"/resources/upload/"+fileName);  
	    return "post/result";  
	}  
	
	/*@RequestMapping(value = "/search",method=RequestMethod.GET)
	public String search(ModelMap map,Map<String,String> searchMap) {
		String province="130000";
		searchMap.put("province",province);
		postService.getPostListBySearchMap(searchMap);
		return "post/add";
	}*/
	
	
	
	/*@RequestMapping(value = "/getPageUtil",method=RequestMethod.POST)
	public String getPageUtil(
		
		@RequestParam("postDetail") String postDetail
	) {
		T user=baseService.getById((Class<E>) User.class,1);
		Post post=new Post();
		post.setPostDetail(postDetail);
		return "post/add";
	}*/
	
	
	
}
